package com.hskn.hss.module.videoconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.module.videoconfig.entity.VideoConfig;
import com.hskn.hss.module.videoconfig.mapper.VideoConfigMapper;
import com.hskn.hss.module.videoconfig.service.IVideoConfigService;
import com.hskn.hss.module.videoconfig.to.VideoConfigTO;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 录像设置
 *
 * @author lny
 * @date 2021-11-18
 */
@Service
public class VideoConfigServiceImpl extends ServiceImpl<VideoConfigMapper, VideoConfig> implements IVideoConfigService {
    @Autowired
    private VideoConfigMapper videoConfigMapper;

    @Override
    public VideoConfig selectVideoConfig() {
        return videoConfigMapper.selectVideoConfig();
    }

    @Transactional
    @Override
    public AjaxResult save(VideoConfigTO videoPlanTO) {
        try {
            List<VideoConfig> list = videoConfigMapper.selectList(new QueryWrapper<>());
            if (CollectionUtils.isEmpty(list)) {
                VideoConfig videoConfig = new VideoConfig();
                BeanUtils.copyProperties(videoPlanTO, videoConfig);
                videoConfig.setId(StringUtils.uuid());
                videoConfigMapper.insert(videoConfig);
            } else {
                VideoConfig videoConfig = list.get(0);
                videoConfig.setLimitDay(videoPlanTO.getLimitDay());
                videoConfig.setLimitSpace(videoPlanTO.getLimitSpace());
                videoConfigMapper.updateById(videoConfig);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return AjaxResult.fail(e.getMessage());
        }
        return AjaxResult.success();
    }
}
